<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Generic controller for entity class, CI AR.
 * @author hmQuan
 *
 */
class Tag extends MY_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model('t_tag');

	}

	public function get_list_tag(){
		$post = $this->input->post();
		$post = parent::check_post_value($post);

		$arrTag = $this->t_tag->get_all();
		parent::print_result($arrTag, SUCCESS_VALUE);exit();
	}

	public function get_tag_by_input_name(){
		$post = $this->input->post();
		$post = parent::check_post_value($post);

		if(isset($post['key'])){
			$key = $post['key'];
		}else{
			$error = 'leng so short';
			parent::print_result($error);exit();
		}

		if(isset($post[CATEGORY_ID])){
			$category_id = $post[CATEGORY_ID];	
		}else{
			$category_id = -1;
		}
		
		$arrForTagNameRoot = array();
		$arrForProductNameRoot= array();
		$arrForTagProductValueRoot= array();

		$arrSearchKey = explode(" " ,$key);

		for($i = 0 ; $i< count($arrSearchKey) ; $i ++ ) {
			// load theo tag name
			$arrForTagName = $this->t_tag->get_data_by_property(array(TAG_TAGNAME .' LIKE ' => '%'.$key.'%'));

			if($arrForTagName == null || count($arrForTagName) == 0){
				$arrForTagName = array();
			}
			$arrForTagNameRoot = array_merge($arrForTagNameRoot , $arrForTagName);
			
			// load theo product name
			$this->load->model('t_product');

			if(isset($post[LONGITUDE]) && isset($post[LATITUDE])){
				
				$long = $post[LONGITUDE];
				$lat = $post[LATITUDE];

				if($category_id != -1){
					$arrForProductName = $this->t_product->get_product_distance($lat , $long ,PRODUCT_NAME .' LIKE '.'\'%'.$key.'%\' AND '.CATEGORY_ID.' ='.$category_id);	
				}else{
					$arrForProductName = $this->t_product->get_product_distance($lat , $long ,PRODUCT_NAME .' LIKE '.'\'%'.$key.'%\'');	
				}
				
			}else{
				if($category_id != -1){
					$arrForProductName = $this->t_product->get_data_by_property(array(PRODUCT_NAME .' LIKE ' => '%'.$key.'%' , CATEGORY_ID => $category_id));
				}else{
					$arrForProductName = $this->t_product->get_data_by_property(array(PRODUCT_NAME .' LIKE ' => '%'.$key.'%'));
				}
			}
			
			if($arrForProductName == null || count($arrForProductName) == 0){
				$arrForProductName = array();
			}
			$arrForProductNameRoot = array_merge($arrForProductNameRoot , $arrForProductName );
			// set theo product value tag
			$this->load->model('t_producttag');
			$arrForTagProductValue = $this->t_producttag->get_data_by_property((array(PRO_TAG_VALUE.' LIKE ' => '%'.$key.'%')));
			if($arrForTagProductValue == null || count($arrForTagProductValue) == 0){
				$arrForTagProductValue = array();
			}
			$arrForTagProductValueRoot = array_merge($arrForTagProductValueRoot , $arrForProductName );
		}

		

		$array_result = array(T_PRODUCT=>$arrForProductNameRoot , T_TAG=> $arrForTagNameRoot , T_PRODUCT_TAG=>$arrForTagProductValueRoot);

		parent::print_result($array_result, SUCCESS_VALUE);exit();

	}

	public function get_tag_by_id(){
		$post = $this->input->post();
		$post = parent::check_post_value($post);

		if(!isset($post[TAG_ID]) || empty($post[TAG_ID])){
			parent::print_result('missing key or value ' . TAG_ID);exit();
		}else{
			$tag_id = $post[TAG_ID];
		}

		$tag = $this->t_tag->get_data_by_id($tag_id);

		if($tag == 0 || count($tag) == 0){
			parent::print_result('Tag id does not exist');
		}else{
			parent::print_result($tag, SUCCESS_VALUE);
		}
		exit();
	}
}